package servlet;

import bean.entity.Boss;
import bean.entity.Customer;
import bean.entity.Developer;
import bean.entity.Service;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String identity=request.getParameter("identity");
        switch (identity)
        {
            case "customer":
                logOutCustomer(request,response);
                break;
            case "service":
                logOutService(request,response);
                break;
            case "developer":
                logOutDeveloper(request,response);
                break;
            case "boss":
                logOutBoss(request,response);
                break;
        }
    }

    private void logOutCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //用户登出
        ServletContext context=getServletContext();
        String name= (String) context.getAttribute("cname");
        boolean tag=false;
        tag=new Customer().logOut(name);
        request.setAttribute("tag",tag);
        if(tag) request.getRequestDispatcher("user_login.jsp").forward(request,response);
        else request.getRequestDispatcher("personal.jsp").forward(request,response);
    }
    private void logOutService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //客服登出
        ServletContext context=getServletContext();
        String name= (String) context.getAttribute("sname");
        boolean tag=false;
        tag=new Service().logOUt(name);
        request.setAttribute("tag",tag);
        if(tag) request.getRequestDispatcher("service_login.jsp").forward(request,response);
        else request.getRequestDispatcher("service_platform.jsp").forward(request,response);
    }
    private void logOutDeveloper(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //开发者登出
        ServletContext context=getServletContext();
        String name= (String) context.getAttribute("devname");
        boolean tag=false;
        tag=new Developer().logOUt(name);
        request.setAttribute("tag",tag);
        if(tag) request.getRequestDispatcher("dev_login.jsp").forward(request,response);
        else request.getRequestDispatcher("dev_platform.jsp").forward(request,response);
    }
    private void logOutBoss(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //Boss登出
        ServletContext context=getServletContext();
        String name= (String) context.getAttribute("bname");
        boolean tag=false;
        tag=new Boss().logOUt(name);
        request.setAttribute("tag",tag);
        if(tag) request.getRequestDispatcher("boss_login.jsp").forward(request,response);
        else request.getRequestDispatcher("boss_platform.jsp").forward(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        super.doPost(req, resp);
    }
}
